package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.OtherCheaper;
import net.skyedu.bean.SurplusBalance;
import net.skyedu.dao.AjaxDAO;

public class SurplusBalanceList  extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
              doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  response.setCharacterEncoding("utf-8");
		  	int currentpage = Integer.parseInt(request.getParameter("currentPage"));
	  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));		
	  		int terId = Integer.parseInt(request.getParameter("terId"));
	  		int stuId = Integer.parseInt(request.getParameter("stuId"));
	  		int type=Integer.parseInt(request.getParameter("subClassType"));
//	  		if(subName != null && !"".equals(subName)){
//	  			subName = URLDecoder.decode(subName,"UTF-8");
//			}
//	  		int claId = Integer.parseInt(request.getParameter("claId"));
	  		
	  		int startIndex = 0;
	  		try{
        	  startIndex = (currentpage - 1) * pagesize;
        	  
        	  AjaxDAO ad = new AjaxDAO();
		      StringBuffer jsonAttr = new StringBuffer("[");
		      int pageCount = 0;
		      //学生查询
//		      	List<AttInfo> list = ad.list(terId,stuId, claId, startIndex, pagesize, 1);
		      	List<SurplusBalance> list = ad.listSurplusBlance(terId,stuId, startIndex, pagesize, 1,type);;
        		pageCount = ad.listSurplusBlance(terId,stuId, startIndex, pagesize, 0,type).size();
        		  if(pageCount != 0){
    			      for(int i = 0; i < list.size(); i++){
    			    	  SurplusBalance att = list.get(i);
    			    	  jsonAttr.append("{");
    			    	  jsonAttr.append("\"stuName\":\""+att.getStuName()+"\",");
//    			    	  jsonAttr.append("\"subjectName\":\""+att.getSubjectName()+"\",");
    			    	  jsonAttr.append("\"termName\":\""+att.getTermName()+"\",");
    			    	  jsonAttr.append("\"balance\":\""+att.getBalance()+"\",");
    			    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
    			    	  jsonAttr.append("}");
    			    	  if(i<list.size()-1){
    			    		  jsonAttr.append(",");
    			    	  }
    			       }
        	  }else{
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
		    	  jsonAttr.append("}");
		      }
		      
		      jsonAttr.append("]");
//		      System.out.println("JSON:---"+jsonAttr);
		      PrintWriter out = response.getWriter();
		      out.print(jsonAttr);
		      out.flush();
		      out.close();
        	
          }catch(Exception e){
        	  //写到日志表中
        	  e.printStackTrace();
          }
       
		
		
		
		
	
	}
}
